

<%@page import="java.awt.Font"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.Graphics"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.util.Random"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%!
    Color getColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }
%>
<%
    try {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expries", 0);
        int width = 60;
        int height = 20;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        OutputStream outputStream = response.getOutputStream();
        Graphics g = image.getGraphics();
        Random random = new Random();
        g.setColor(getColor(200, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
        g.setColor(getColor(150, 200));
        for (int i = 0; i < 140; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(12);
            int y2 = random.nextInt(12);
            g.drawLine(x1, y1, x2, y2);
        }
        String str = "";
        Cookie[] cookies = request.getCookies();
        for (int j = 0; j < cookies.length; j++) {
            if (cookies[j].getName().equals("imgstr")) {
                str = cookies[j].getValue();
            }
        }
        if (!str.equals("")) {
            for (int i = 0; i < str.length(); i++) {
                //char[] string = str.toCharArray();
                g.setColor(getColor(20, 140));
                g.drawString(str.substring(i, i+1), 12 * i + 5, 16);
                //g.drawString(str.charAt(i)+"", 12 * i + 5, 16);
            }
        }
        g.dispose();
        ImageIO.write(image, "jpg", outputStream);
        outputStream.flush();
        outputStream.close();
        response.flushBuffer();
        out.clear();
        //out.close();
        out = pageContext.pushBody();
    } catch (Exception e) {
        out.println(e.getMessage());
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
    </body>
</html>
